package com.outsourcing.bottle.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.outsourcing.bottle.domain.PasterDirConfigEntry;
import com.outsourcing.bottle.util.AppContext;
/**
 * 贴纸目录
 * @author ting
 *
 */
public class StickersPackTable {

	private final String TABLE_STICK_DIR = "stick_dir";
	private DBManager db = null;
	public StickersPackTable() {
		if (db == null) {
			db = DBManager.get();
		}
		if (!db.isTableExits(db.getConnection(), TABLE_STICK_DIR)) {
			createStickersPack();
		}
	}
	
	private void createStickersPack() {
		String createSql = "create table if not exists "+TABLE_STICK_DIR+" (id integer primary key autoincrement,login_uid integer, pasterdir_id integer, pasterdir_name_cn varchar, pasterdir_name_en varchar, pasterdir_coverurl varchar,credit integer,ttcredit integer,btf_level integer,useterm varchar,unlocked integer)";
		db.creatTable(db.getConnection(), createSql, TABLE_STICK_DIR);
	}
	
	public ArrayList<PasterDirConfigEntry> getStickDir() {
		Cursor cursor = null;
		ArrayList<PasterDirConfigEntry> stickDirConfigEntries = null;
		try {
			cursor = db.find(db.getConnection(), "select * from " + TABLE_STICK_DIR +" where login_uid =?", new String[]{AppContext.getInstance().getLogin_uid()+""});
			if (cursor != null) {
				stickDirConfigEntries = new ArrayList<PasterDirConfigEntry>();
				while (cursor.moveToNext()) {
					PasterDirConfigEntry stickDirConfigEntry = new PasterDirConfigEntry();
					stickDirConfigEntry.setPasterdir_id(cursor.getInt(cursor.getColumnIndex("pasterdir_id")));
					stickDirConfigEntry.setPasterdir_name_cn(cursor.getString(cursor.getColumnIndex("pasterdir_name_cn")));
					stickDirConfigEntry.setPasterdir_name_en(cursor.getString(cursor.getColumnIndex("pasterdir_name_en")));
					stickDirConfigEntry.setPasterdir_coverurl(cursor.getString(cursor.getColumnIndex("pasterdir_coverurl")));
					stickDirConfigEntry.setCredit(cursor.getInt(cursor.getColumnIndex("credit")));
					stickDirConfigEntry.setTtcredit(cursor.getInt(cursor.getColumnIndex("ttcredit")));
					stickDirConfigEntry.setBtf_level(cursor.getInt(cursor.getColumnIndex("btf_level")));
					stickDirConfigEntry.setUseterm(cursor.getString(cursor.getColumnIndex("useterm")));
					stickDirConfigEntry.setUnlocked(cursor.getInt(cursor.getColumnIndex("unlocked")));
					stickDirConfigEntries.add(stickDirConfigEntry);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return stickDirConfigEntries;
	}
	
	public void upStickDirlocked(int pasterdir_id){
//		try {
//			String sql = "update table "+TABLE_STICK_DIR+" set unlocked=? where pasterdir_id=?";
//			db.update(db.getConnection(), sql, new String[] {String.valueOf(1),String.valueOf(pasterdir_id)} );
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		
		db.getConnection().beginTransaction();
		try{
			db.getConnection().execSQL("update "+TABLE_STICK_DIR+" set unlocked=? where pasterdir_id=? and login_uid=?",
					new Object[]{1,pasterdir_id,AppContext.getInstance().getLogin_uid()});
			db.getConnection().setTransactionSuccessful();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			db.getConnection().endTransaction();
		}
		
	}
	
	
	public void saveStickDir(PasterDirConfigEntry stickDirConfigEntry) {
		ContentValues values = new ContentValues();
		values.put("login_uid", AppContext.getInstance().getLogin_uid());
		values.put("pasterdir_id", stickDirConfigEntry.getPasterdir_id());
		values.put("pasterdir_name_cn", stickDirConfigEntry.getPasterdir_name_cn());
		values.put("pasterdir_name_en", stickDirConfigEntry.getPasterdir_name_en());
		values.put("pasterdir_coverurl", stickDirConfigEntry.getPasterdir_coverurl());
		values.put("credit", stickDirConfigEntry.getCredit());
		values.put("ttcredit", stickDirConfigEntry.getTtcredit());
		values.put("btf_level", stickDirConfigEntry.getBtf_level());
		values.put("useterm", stickDirConfigEntry.getUseterm());
		values.put("unlocked", stickDirConfigEntry.getUnlocked());
		db.save(db.getConnection(), TABLE_STICK_DIR, values);
	}
	
	public void clearTalbe() {
		db.delete(db.getConnection(), TABLE_STICK_DIR, null, null);
	}
}
